using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Primitives2d;
using GeneradorPosiciones;

namespace CrowdSimulations
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        List<Point> Puntos;
        Primitives2d.Primitives2d Lienzo;
        Pos_Gen Generador;
        bool Termino;

        List<Point> P_Fin;

        MouseState lastMouseState;
        MouseState currentMouseState;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            Termino = false;
            Puntos = new List<Point>();
            this.IsMouseVisible = true;
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Lienzo = new Primitives2d.Primitives2d(GraphicsDevice, spriteBatch);
            Generador = new Pos_Gen();
        }

        protected override void UnloadContent()
        {

        }

        protected override void Update(GameTime gameTime)
        {
            
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            // The active state from the last frame is now old
            lastMouseState = currentMouseState;

            // Get the mouse state relevant for this frame
            currentMouseState = Mouse.GetState();

            if (lastMouseState.LeftButton == ButtonState.Released && currentMouseState.LeftButton == ButtonState.Pressed && !Termino)
            {
                Puntos.Add(new Point(Mouse.GetState().X, Mouse.GetState().Y));
            }

            if (lastMouseState.RightButton == ButtonState.Released && currentMouseState.RightButton == ButtonState.Pressed && !Termino)
            {
                Termino = true;
                P_Fin = Generador.Gen_Formation_A(Puntos);

            }

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();

            if (Puntos.Count > 0)
            {
                for (int i = 0; i < Puntos.Count - 1; ++i)
                    Lienzo.DrawLine(Puntos[i], Puntos[i + 1], Color.Black);

                if (Termino)
                {
                    Lienzo.DrawLine(Puntos[Puntos.Count - 1], Puntos[0], Color.Black);
                    foreach(Point p in P_Fin)
                        Lienzo.DrawCircle(p, 5, Color.Red);
                }
                else
                    Lienzo.DrawLine(Puntos[Puntos.Count - 1], new Point(Mouse.GetState().X, Mouse.GetState().Y), Color.Black);
            }
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
